package me.zhengjie.modules.houseshowscenehot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.houseshowscenehot.domain.HouseShowSceneHot;
import me.zhengjie.modules.houseshowscenehot.service.HouseShowSceneHotService;
import me.zhengjie.modules.houseshowscenehot.service.dto.HouseShowSceneHotDto;
import me.zhengjie.modules.houseshowscenehot.service.dto.HouseShowSceneHotQueryParam;
import me.zhengjie.modules.houseshowscenehot.service.mapper.HouseShowSceneHotMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2021-12-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = HouseShowSceneHotService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HouseShowSceneHotServiceImpl extends CommonServiceImpl<HouseShowSceneHotMapper, HouseShowSceneHot> implements HouseShowSceneHotService {

    // private final RedisUtils redisUtils;
    private final HouseShowSceneHotMapper houseShowSceneHotMapper;

    @Override
    public PageInfo<HouseShowSceneHotDto> queryAll(HouseShowSceneHotQueryParam query, Pageable pageable) {
        IPage<HouseShowSceneHot> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<HouseShowSceneHot> page = houseShowSceneHotMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, HouseShowSceneHotDto.class);
    }

    @Override
    public List<HouseShowSceneHotDto> queryAll(HouseShowSceneHotQueryParam query){
        return ConvertUtil.convertList(houseShowSceneHotMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), HouseShowSceneHotDto.class);
    }

    @Override
    public HouseShowSceneHot getById(Long id) {
        return houseShowSceneHotMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public HouseShowSceneHotDto findById(Long id) {
        return ConvertUtil.convert(getById(id), HouseShowSceneHotDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HouseShowSceneHotDto resources) {
        HouseShowSceneHot entity = ConvertUtil.convert(resources, HouseShowSceneHot.class);
        return houseShowSceneHotMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(HouseShowSceneHotDto resources){
        HouseShowSceneHot entity = ConvertUtil.convert(resources, HouseShowSceneHot.class);
        int ret = houseShowSceneHotMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return houseShowSceneHotMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<HouseShowSceneHotDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (HouseShowSceneHotDto houseShowSceneHot : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("场景ID", houseShowSceneHot.getHouseShowSceneId());
              map.put("热点名称", houseShowSceneHot.getText());
              map.put("定位参数，X轴（左耳到右耳）旋转（正数往上，负数往下）", houseShowSceneHot.getPitch());
              map.put("定位参数，y轴（从脚到头）旋转", houseShowSceneHot.getYaw());
              map.put("定位参数，视野水平高度", houseShowSceneHot.getRoll());
              map.put("热点类型，scene 场景切换热点； info 信息展示；URL 以热点为链接，跳转到其他页面的url", houseShowSceneHot.getType());
              map.put("切换到的下一个场景ID,当"type" = "scene"时使用", houseShowSceneHot.getSceneId());
              map.put("与"sceneId"值一致,当"type" = "scene"时使用", houseShowSceneHot.getClickHandlerArgs());
              map.put("视频地址,当"type" = "info"时使用，video、image、URL三选一", houseShowSceneHot.getVideo());
              map.put("图片地址,当"type" = "info"时使用，video、image、URL三选一", houseShowSceneHot.getImage());
              map.put("跳转地址,当"type" = "info"时使用，video、image、URL三选一", houseShowSceneHot.getUrl());
              map.put("热点图标样式，向前为空，向右为"pnlm-hotspot pnlm-sprite pnlm-scene right"， 向左为"pnlm-hotspot pnlm-sprite pnlm-scene left",默认为向前", houseShowSceneHot.getCssClass());
              map.put("更新时间", houseShowSceneHot.getUpdateTime());
              map.put("创建时间", houseShowSceneHot.getCreateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
